package com.syh.creditsservice.controller;

import com.syh.bscommon.Util.UserHolder;
import com.syh.bscommon.pojo.Result;
import com.syh.bscommon.pojo.Transactions;
import com.syh.creditsservice.pojo.CarbonCredits;
import com.syh.creditsservice.service.ICarbonCreditsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author syh
 * @since 2024-07-02
 */
@Tag(name = "CarbonCreditsController", description = "公司碳额度模块")
@RestController
@RequestMapping("/carbon-credits")
@RequiredArgsConstructor
public class CarbonCreditsController {
    private final ICarbonCreditsService carbonCreditsService;

    @GetMapping("/getCreditAmount")
    @Operation(summary = "获取公司额度")
    public Result getCreditAmount() {
        BigDecimal creditAmount = carbonCreditsService.getCreditAmount(UserHolder.getUser().getCompanyId());
        return Result.success(creditAmount);
    }

    //公司余额获取
    @GetMapping("/getAccount")
    @Operation(summary = "获取公司余额")
    public Result getAccount() {
        BigDecimal creditAmount = carbonCreditsService.getAccount(UserHolder.getUser().getCompanyId());
        return Result.success(creditAmount);
    }

    //更新公司余额
    @PutMapping("/setAccount")
    @Operation(summary = "更新公司余额")
    @Transactional
    public Result setAccount(@RequestBody CarbonCredits carbonCredits) {
        carbonCredits.setCompanyId(UserHolder.getUser().getCompanyId());
        carbonCreditsService.updateAccount(carbonCredits);
        return Result.success();
    }

    @PostMapping("/setinfo")
    @Operation(summary = "初始化公司信息，注册进行")
    @Transactional
    public Result setinfo(@RequestBody CarbonCredits carbonCredits) {
        carbonCreditsService.save(carbonCredits);
        return Result.success();
    }

    //    -----feign--------
    @GetMapping("/getCreditAmount_f")
    @Operation(summary = "feign获取公司额度")
    public BigDecimal f_getCreditAmount(@RequestParam Integer companyId) {
        return carbonCreditsService.getCreditAmount(companyId);
    }
    @PutMapping("/updateCreditAmount")
    @Operation(summary = "feign更新公司碳额度-")
    @Transactional
    public void updateCreditAmount(@RequestParam Integer companyId, @RequestParam BigDecimal emissionAmount) {
        carbonCreditsService.updateCreditAmount(companyId, emissionAmount);
    }
    @PutMapping("/updateCreditAmountAdd")
    @Operation(summary = "feign更新公司碳额度+")
    @Transactional
    public void updateCreditAmountAdd(@RequestParam Integer companyId, @RequestParam BigDecimal emissionAmount) {
        carbonCreditsService.updateCreditAmountAdd(companyId, emissionAmount);
    }



    @PutMapping("/updateAccount_a")
    @Operation(summary = "feign更新卖家公司余额+")
    @Transactional
    public void updateAccount_a(@RequestBody Transactions transactions) {
        carbonCreditsService.updateAccount_a(transactions);
    }
    @PutMapping("/updateAccount_s")
    @Operation(summary = "feign更新买家公司余额-")
    @Transactional
    public void updateAccount_s(@RequestBody Transactions transactions) {
        carbonCreditsService.updateAccount_s(transactions);
    }
    @PutMapping("/updateAccount_add_buy")
    @Operation(summary = "feign更新买家公司余额退款+")
    @Transactional
    public void updateAccount_add_buy(@RequestBody Transactions transactions) {
        carbonCreditsService.updateAccount_add_buy(transactions);
    }
    @PutMapping("/updateAccount_add_sell")
    @Operation(summary = "feign更新买家公司余额退款-")
    @Transactional
    public void updateAccount_add_sell(@RequestBody Transactions transactions) {
        carbonCreditsService.updateAccount_add_sell(transactions);
    }
}
